package com.css.fxfzxczh.modules.actionTrack.service.impl;


import com.css.fxfzxczh.constants.XczhConstants;
import com.css.fxfzxczh.modules.actionTrack.entity.ActionTrackEntity;
import com.css.fxfzxczh.modules.actionTrack.repository.ActionTrackRepository;
import com.css.fxfzxczh.modules.actionTrack.service.ActionTrackService;
import com.css.fxfzxczh.util.GeoUtil;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import com.css.fxfzxczh.util.UUIDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author
 * @program
 * @description
 * @create 2022/8/27 10:17
 **/
@Service
public class ActionTrackServiceImpl implements ActionTrackService {
    @Autowired
    ActionTrackRepository actionTrackRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(ActionTrackEntity actionTrackEntity) throws Exception{
        //当同一个用户重复记录轨迹时删除上一次记录的足迹

        //查询当前与当前用户记录的足迹信息，有没有结束，如果结束代表，该用户不是第一次记录轨迹
        String userId= actionTrackRepository.getLastFoot(actionTrackEntity.getUserId(), XczhConstants.END_POINT,actionTrackEntity.getXsemId());
        //那么根据用户id删除上一次记录的足迹信息
        if(PlatformObjectUtils.isNotEmpty(userId)){
            actionTrackRepository.deleteByUserId(userId);
        }


        String id = UUIDGenerator.getUUID();
        actionTrackEntity.setCreateTime(new Date());
        actionTrackEntity.setFootId(id);
        actionTrackEntity.setGeom("POINT("+actionTrackEntity.getLongitude().toString()+" " +actionTrackEntity.getLatitude().toString()+")");
        /*if(!"0".equals(actionTrackEntity.getPhasePoint())){
            ActionTrackEntity lastActionTrack=actionTrackRepository.fingByFootId(actionTrackEntity.getLastFootId());
            double lastLongitude = lastActionTrack.getLongitude().doubleValue();
            double lastLatitude = lastActionTrack.getLatitude().doubleValue();
            double longitude = actionTrackEntity.getLongitude().doubleValue();
            double latitude = actionTrackEntity.getLatitude().doubleValue();
            Double distance = GeoUtil.GetDistance(longitude, latitude, lastLongitude, lastLatitude);
            if(Double.doubleToLongBits(distance) >= Double.doubleToLongBits(20.0) && Double.doubleToLongBits(distance) <= Double.doubleToLongBits(30.0) ) {
                actionTrackRepository.save(actionTrackEntity);
            }else{
                throw new RuntimeException("当前轨迹点距离上一个轨迹点不在20米~30米之间，保存失败");
            }
        }else{*/
            actionTrackRepository.save(actionTrackEntity);
        //}
    }

    @Override
    public List<ActionTrackEntity> getActionTracks(String userId, String nowTime,String xsemId) {
        List<ActionTrackEntity> ActionTrackS=actionTrackRepository.getActionTracks( userId, nowTime,xsemId);
        return ActionTrackS;
    }

    @Override
    public List<ActionTrackEntity> getAllPersonnelPosition(String xsemId) {

        return actionTrackRepository.getAllPersonnelPosition(xsemId);
    }

    @Override
    public List<ActionTrackEntity> getLeaderPosition(String xsemId) {
        return actionTrackRepository.getLeaderPosition( xsemId);
    }
}
